import React, {Component} from 'react'
import {ListComponent,ListBox} from '../../../components'
import {fetchData} from '../../../components/AjaxConsumer'
import { Form, Modal, Button } from 'antd';

export default class ModelModalTable extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false,
            rowObj: {},
            isAdd: true
        }
        this.selectRowObj = {};
        this.rowOperation = [
            {
                handleName: 'rowClick',
                showName: '行单击选择事件'
            }
        ];
        //header 操作列表
        this.headerOperation = [

        ];

        //table列表展示列配置
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏
        this.tableColumn = {
            storageKey: 'modelId',
            data: [
                {
                    showName: 'modelId',
                    columnName: 'modelId',
                    columnHidden:true,
                    foreverHidden: true
                },{
                    showName: '型号代码',
                    columnName: 'model'
                },
                {
                    showName: '型号类型',
                    columnName: 'modelType'
                },
                {
                    showName: '型号描述',
                    columnName: 'modelDescZh'
                },{
                    showName: '类型',
                    columnName: 'type',
                    showOption: [//页面显示配置
                        {
                            optionKey: '0',
                            optionShow: '正品'
                        }, {
                            optionKey: '1',
                            optionShow: '样品'
                        }
                    ]
                },
                {
                    showName: '外径',
                    columnName: 'outsideDiameter'
                }, {
                    showName: '壁厚',
                    columnName: 'thickness'
                }, {
                    showName: '单位',
                    columnName: 'unit'
                }, {
                    showName: '创建人',
                    columnName: 'createBy'
                }, {
                    showName: '创建时间',
                    columnName: 'gmtCreate'
                }
            ]
        };

        //搜索属性配置
        this.searchComponentData = [
            {
                showName: '类型',
                name: 'type',
                type: 'select',
                optionList: [//下拉框的信息
                    //下拉框的信息
                    {
                        optionKey: '',
                        optionShow: ''
                    }, {
                        optionKey: '0',
                        optionShow: '正品'
                    }, {
                        optionKey: '1',
                        optionShow: '样品'
                    }
                ]
            },
            {
                showName: '型号类型',
                name: 'modelType',
                type: 'select',
                optionList: [//下拉框的信息
                ]
            }
        ];

    }

    rowHandles = (strHandles, rowObj) => {
        if(strHandles == 'rowClick'){
            this.rowClickHandle(rowObj);
        }
    }

    rowClickHandle = (rowObj) => {
        this.selectRowObj = rowObj[0].data;
    }

    showModal = () => {
        this.setState({
            visible: true
        })

        this.selectRowObj = {};

    }

    handleOk = () => {
        this.setState({
            visible: false
        })

        let resultData = {};
        resultData.model = this.selectRowObj.model;
        resultData.modelType = this.selectRowObj.modelType;
        resultData.modelDescZh = this.selectRowObj.modelDescZh;
        resultData.unit = this.selectRowObj.unit;

        this.props.getTableData(resultData);
    }

    handleCancel = () => {
        this.setState({
            visible: false
        })
    }

    componentDidMount() {
        const dicType = 'model_type';
        const options= {
            url: '/base/dic/get/' + dicType,
            type: 'get',
            condition:{}
        };
        //先写死，后面再改
        fetchData(this, options, this.dicInitCallBackHandle);
    }

    //下拉框 初始化
    dicInitCallBackHandle = (resData)=>{
        //alert('resData' + JSON.stringify(resData, null, 4));
        let optionList = [];
        let tmp = {};
        tmp.optionKey = '';
        tmp.optionShow = '';
        optionList.push(tmp);
        resData.data.map( (e,index) => {
            let tmp = {};
            tmp.optionKey = e.dicCode;
            tmp.optionShow = e.dicName;
            optionList.push(tmp);
        });

        //alert('optionList' + JSON.stringify(optionList, null, 4));
        this.tableColumn.data[2].showOption = optionList;
        this.searchComponentData[1].optionList = optionList;
        //this.setState({
        //    tableColumn: this.tableColumn,
        //    searchComponentData: this.searchComponentData
        //})

    }

    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;

        return(
            <Modal
                    style={{top: '5px'}}
                    visible={this.state.visible}
                    title="原材料型号明细"
                    onOk={this.handleOk}
                    Height={'300px'}
                    onCancel={this.handleCancel}
                    width={'1250px'}
                    footer={[
                        <Button key="back" onClick={this.handleCancel}>取消</Button>,
                        <Button key="submit" type="primary" onClick={this.handleOk}>
                        确定
                        </Button>,
                    ]}
                    maskClosable={false}
            >
                <ListBox
                        ref = 'listLocation'
                        isHideLeft={isHideLeft}
                        tableColumnData = {this.tableColumn}
                        searchComponentData={this.searchComponentData}
                        url = '/base/data/model/not/whiteblack/list'
                        rowHandles = {this.rowHandles}
                        rowOperation = {this.rowOperation}
                        headerOperation = {this.headerOperation}
                        subtractHeight="80"
                        isRowSingle = "true"
                />

            </Modal>
        )
    }
}
